import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class SortComparison {
    public static int cardCompare(String card1, String card2) {
        int value1 = getCardValue(card1);
        int value2 = getCardValue(card2);
        char suit1 = card1.charAt(card1.length() - 1);
        char suit2 = card2.charAt(card2.length() - 1);

        if (suitPriority(suit1) < suitPriority(suit2)) {
            return -1;
        } else if (suitPriority(suit1) > suitPriority(suit2)) {
            return 1;
        } else {
            if (value1 < value2) {
                return -1;
            } else if (value1 > value2) {
                return 1;
            } else {
                return 0;
            }
        }
    }

    private static int getCardValue(String card) {
        String valueStr = card.substring(0, card.length() - 1);
        if (valueStr.equals("A")) {
            return 1;
        } else if (valueStr.equals("J")) {
            return 11;
        } else if (valueStr.equals("Q")) {
            return 12;
        } else if (valueStr.equals("K")) {
            return 13;
        } else {
            return Integer.parseInt(valueStr);
        }
    }

    private static int suitPriority(char suit) {
        if (suit == 'H') {
            return 1;
        } else if (suit == 'C') {
            return 2;
        } else if (suit == 'D') {
            return 3;
        } else {
            return 4;
        }
    }

    public static ArrayList<String> bubbleSort(ArrayList<String> array) {
        int n = array.size();
        for (int i = 0; i < n - 1; i++) {
            for (int j = 0; j < n - i - 1; j++) {
                if (cardCompare(array.get(j), array.get(j + 1)) > 0) {
                    String temp = array.get(j);
                    array.set(j, array.get(j + 1));
                    array.set(j + 1, temp);
                }
            }
        }
        return array;
    }

    public static ArrayList<String> mergeSort(ArrayList<String> array) {
        if (array.size() <= 1) {
            return array;
        }
        int mid = array.size() / 2;
        ArrayList<String> left = new ArrayList<>(array.subList(0, mid));
        ArrayList<String> right = new ArrayList<>(array.subList(mid, array.size()));

        left = mergeSort(left);
        right = mergeSort(right);

        return merge(left, right);
    }

    private static ArrayList<String> merge(ArrayList<String> left, ArrayList<String> right) {
        ArrayList<String> result = new ArrayList<>();
        int i = 0, j = 0;
        while (i < left.size() && j < right.size()) {
            if (cardCompare(left.get(i), right.get(j)) <= 0) {
                result.add(left.get(i));
                i++;
            } else {
                result.add(right.get(j));
                j++;
            }
        }
        result.addAll(left.subList(i, left.size()));
        result.addAll(right.subList(j, right.size()));
        return result;
    }

    public static long measureBubbleSort(String filename) throws FileNotFoundException {
        ArrayList<String> cards = readCardsFromFile(filename);
        long startTime = System.currentTimeMillis();
        bubbleSort(cards);
        long endTime = System.currentTimeMillis();
        return endTime - startTime;
    }

    public static long measureMergeSort(String filename) throws FileNotFoundException {
        ArrayList<String> cards = readCardsFromFile(filename);
        long startTime = System.currentTimeMillis();
        mergeSort(cards);
        long endTime = System.currentTimeMillis();
        return endTime - startTime;
    }

    private static ArrayList<String> readCardsFromFile(String filename) throws FileNotFoundException {
        ArrayList<String> cards = new ArrayList<>();
        File file = new File(filename);
        Scanner scanner = new Scanner(file);
        while (scanner.hasNextLine()) {
            cards.add(scanner.nextLine());
        }
        scanner.close();
        return cards;
    }

    public static void sortComparison(String[] filenames) throws FileNotFoundException {
        try (java.io.FileWriter writer = new java.io.FileWriter("sortComparison.csv")) {
            writer.write(",");
            for (String filename : filenames) {
                writer.write(filename.split("\\.")[0].split("sort")[1] + ",");
            }
            writer.write("\n");

            writer.write("bubbleSort,");
            for (String filename : filenames) {
                long time = measureBubbleSort(filename);
                writer.write(time + ",");
            }
            writer.write("\n");

            writer.write("mergeSort,");
            for (String filename : filenames) {
                long time = measureMergeSort(filename);
                writer.write(time + ",");
            }
            writer.write("\n");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
